package com.java.second_goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.second_goods.common.utils.StringUtils;
import com.java.second_goods.entity.Promotion;
import com.java.second_goods.mapper.PromotionMapper;
import com.java.second_goods.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 活动 服务层实现
 */
@Service
public class PromotionServiceImpl extends ServiceImpl<PromotionMapper, Promotion> implements PromotionService {

    @Autowired
    private PromotionMapper promotionMapper;

    /**
     * 查询活动信息
     *
     * @param id 活动ID
     * @return 活动信息
     */
    @Override
    public Promotion selectPromotionById(Integer id) {
        return promotionMapper.selectById(id);
    }

    /**
     * 查询活动列表
     *
     * @param promotion 活动信息
     * @return 活动集合
     */
    @Override
    public List<Promotion> selectPromotionList(Promotion promotion) {
        LambdaQueryWrapper<Promotion> queryWrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        if (promotion != null) {
            if (StringUtils.isNotEmpty(promotion.getTitle())) {
                queryWrapper.like(Promotion::getTitle, promotion.getTitle());
            }
            if (StringUtils.isNotEmpty(promotion.getType())) {
                queryWrapper.eq(Promotion::getType, promotion.getType());
            }
            if (promotion.getStatus() != null) {
                queryWrapper.eq(Promotion::getStatus, promotion.getStatus());
            }
        }
        // 按创建时间降序排列
        queryWrapper.orderByDesc(Promotion::getCreateTime);
        return promotionMapper.selectList(queryWrapper);
    }

    /**
     * 查询有效的活动列表
     *
     * @return 活动列表
     */
    @Override
    public List<Promotion> selectEnabledPromotions() {
        return promotionMapper.selectEnabledPromotions();
    }

    /**
     * 根据类型查询活动列表
     *
     * @param type 类型
     * @return 活动列表
     */
    @Override
    public List<Promotion> selectPromotionsByType(String type) {
        return promotionMapper.selectPromotionsByType(type);
    }

    /**
     * 新增活动
     *
     * @param promotion 活动信息
     * @return 结果
     */
    @Override
    public int insertPromotion(Promotion promotion) {
        // 设置默认值
        if (promotion.getStatus() == null) {
            promotion.setStatus(1);
        }
        return promotionMapper.insert(promotion);
    }

    /**
     * 修改活动
     *
     * @param promotion 活动信息
     * @return 结果
     */
    @Override
    public int updatePromotion(Promotion promotion) {
        return promotionMapper.updateById(promotion);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动ID
     * @return 结果
     */
    @Override
    public int deletePromotionById(Integer id) {
        return promotionMapper.deleteById(id);
    }

    /**
     * 批量删除活动信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePromotionByIds(Integer[] ids) {
        return promotionMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 更新活动状态
     *
     * @param promotionId 活动ID
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updatePromotionStatus(Integer promotionId, Integer status) {
        return promotionMapper.updatePromotionStatus(promotionId, status);
    }
}